package com.netease.nim.uikit.xpopup.popuview

import android.content.Context
import android.graphics.Color
import android.view.View
import android.widget.FrameLayout
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener
import com.bigkoo.pickerview.view.TimePickerView
import com.contrarywind.view.WheelView
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.util.XPopupUtils
import www.wen.com.common.R
import www.wen.com.common.lib.extension.getColorAgency
import www.wen.com.common.lib.extension.setOnClickListenerAgency
import www.wen.com.common.lib.helper.ResultUiCallBack
import www.wen.com.common.lib.util.DateUtil
import java.util.*
import kotlin.properties.Delegates


/**
 * @author WEN
 * @Description: 日期选择器
 * @date 2021/4/25 21:35
 */
class BottomDateSelectPopupView(
        context: Context,
        val timeStr: String?,
        private val resultCallBack: ResultUiCallBack<Long>? = null,
) : BottomPopupView(context), View.OnClickListener {

    private var mTime: Long = 0

    private lateinit var mTvOK: View

    private lateinit var mTvToday: View

    private lateinit var mTimePickerInnerContainer: FrameLayout

    private var mPvTime: TimePickerView by Delegates.notNull()

    override fun getImplLayoutId(): Int = R.layout.popupview_date_select

    override fun getMaxHeight(): Int = (XPopupUtils.getScreenHeight(context) * 0.45f).toInt()

    override fun onCreate() {
        super.onCreate()
        mTvToday = findViewById(R.id.tvToday)
        mTvOK = findViewById(R.id.tvOK)
        mTimePickerInnerContainer = findViewById(R.id.timePickerInnerContainer)
        val selectedDate = Calendar.getInstance()

        mTime = selectedDate.timeInMillis
        if(timeStr!=null){
            try {
                val data = DateUtil.strToTimeStamp(timeStr, format = "yyyy-MM-dd")
                selectedDate.timeInMillis = data
                mTime = selectedDate.timeInMillis
            }catch (e:Exception){ }
        }

        mPvTime = TimePickerBuilder(context) { date, v ->

        }.setLayoutRes(R.layout.pickerview_custom_time) {

        }.setType(booleanArrayOf(true, true, true, false, false, false)).setLabel("", "", "", "", "", "") //设置空字符串以隐藏单位提示   hide label
                .setDividerColor(Color.DKGRAY).setContentTextSize(20).setTimeSelectChangeListener(object : OnTimeSelectChangeListener {

                    override fun onTimeSelectChanged(date: Date?) {
                        mTime = date!!.time
                    }

                }).setLineSpacingMultiplier(2f)
                .setDate(selectedDate)
                .setDividerType(WheelView.DividerType.FILL)
                .setDecorView(mTimePickerInnerContainer)
                .setOutSideColor(getColorAgency(R.color.colorFFFFFF))
                .setBgColor(getColorAgency(R.color.color1B1D22))
                .setTextColorCenter(getColorAgency(R.color.colorFFFFFF))
                .setOutSideCancelable(false).build()
        mPvTime.setKeyBackCancelable(false)

        mPvTime.show(false)

        mPvTime.returnData()

        setOnClickListenerAgency(this, mTvToday, mTvOK)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.tvToday -> { dismiss() }

            R.id.tvOK -> {
                resultCallBack?.invoke(v, mTime, this)
                dismiss()
            }
        }
    }

}